#!/usr/bin/env python

import string

"""
Define functions that serve to decode strings in a sample of backdoor.android.obad.a (F7BE25E4F19A3A82D2E206DE8AC979C8) 
"""

main_service_tab = [
	27, -9, 11, -41, -10, -2, 2, -7,
			23, -19, -49, 73, 1, -9, 5, -60, 32, 37, -10, 0, 13, -5, 26, -21,
			6, -28, 10, -18, 2, 15, -8, 16, -1, -4, -3, -52, 55, 14, 1, 8, -13,
			11, 8, -68, 23, 46, 1, 8, -13, 21, -2, 15, -8, 16, -1, -4, -3, -52,
			55, 14, 1, 8, -13, 11, 8, -68, 39, 23, -5, 19, -11, 1, -18, 36,
			-11, 3, 1, 15, -11, 11, -9, 4, 16, 0, 17, -31, 35, 0, -7, 7, -5,
			-38, 42, 30, -18, -46, 63, -48, 3, 6, -31, 35, 0, -7, 7, -5, 15,
			-8, 16, -1, -4, -3, -52, 67, 6, -67, 22, 53, -10, 5, -6, 15, -8,
			16, -1, -4, -3, -52, 55, 14, 1, 8, -13, 11, 8, -68, 29, 39, 8, -13,
			11, 8, -21, 0, 41, -23, 42, -56, 2, -50, 56, -27, 1, -21, 59, -48,
			18, -3, -18, 30, -1, 10, 6, 30, -12, 24, -46, 50, -39, 10, 26, -54,
			46, -10, -16, 5, 27, -28, 34, -48, 50, -28, 41, 5, -10, 14, 3, 4,
			-37, 11, -7, -6, 10, 16, -4, 24, -13, 18, -3, 19, -18, -27, -14,
			60, -8, 5, -1, 20, -16, 8, -52, -7, 35, 31, -22, -31, 33, 32, -8,
			9, -28, 13, 27, -7, -1, 24, -42, -21, 23, 13, 27, 5, -23, 21, -38,
			41, -9, -8, 31, -9, -1, 21, -19, -55, 39, -27, 29, -26, 32, 9, 0,
			39, -43, -24, 16, 49, -30, -27, 36, -32, 49, -46, 4, 69, -19, -29,
			20, -33, 29, 12, 18, -49, 50, -10, 3, 20, -31, 47, 2, -2, -45, 15,
			7, 13, -27, 39, -38, 42, -4, -46, 21, 44, -23, -34, 20, 2, 8, 10,
			-29, 57, -28, -7, 49, 2, -37, -23, 58, 3, -57, 52, 8, -35, 39, -51,
			0, 4, -1, 51, -24, 11, -24, 4, 12, -18, 6, 30, 11, 2, 32, -64, 21,
			0, 37, -45, 1, 56, -7, 11, -8, -47, 57, -5, -22, -3, -14, 5, 62,
			-60, 52, -5, -41, 33, -31, 1, 25, -23, 58, -6, -41, 33, 31, -46, 5,
			52, -70, 28, -4, 33, -43, 26, 21, -22, 28, -19, 26, -14, 19, 10,
			-38, 3, -21, 42, -19, 50, -11, -22, 28, -32, 47, -14, -18, 36, -64,
			68, -69, 31, -16, 53, 14, -37, 41, -15, -15, -11, -20, 33, 33, -27,
			39, -59, 67, -4, -65, 8, 18, -4, 52, -61, 48, 21, -67, 33, -23, 20,
			-16, 36, -36, 17, -10, 1, 47, -40, -5, -3, 44, 22, -54, 54, 14,
			-43, 39, -30, -21, 71, -68, 51, 10, -23, -24, 68, -51, 19, 32, -61,
			29, -24, -2, -4, 77, -28, -37, 42, -12, -1, 34, 15, -72, 25, -13,
			3, 22, 20, 12, 18, 3, -59, 51, -30, 5, 12, -31, 1, 25, -20, 55, -6,
			-7, 21, -57, 51, -29, 1, 52, -36, 7, 38, -14, -46, -6, 43, -31, 47,
			0, 21, -12, 10, -38, 3, -21, 42, -15, 30, 5, -22, 10, -13, 35, -3,
			-18, 32, -60, 6, 37, -13, -18, 55, 9, -56, 35, 36, -12, -28, -1,
			49, -37, 34, -39, 51, -31, -14, 2, -1, 13, 33, -10, -15, -8, -21,
			50, -8, -16, -13, 46, -25, 43, -46, 58, -8, -8, -23, -26, -3, 61,
			-17, 34, -24, 15, 15, -63, -5, 6, 42, 30, -56, 32, -33, 53, -28, 5,
			11, 33, -42, 11, 35, -38, -21, 53, -13, -28, 34, -8, 44, -15, 15,
			-14, -34, -8, 58, -41, 42, -6, 23, -1, -32, 40, -20, -23, -30, 63,
			14, -66, 68, -6, -6, -12, 3, 21, -24, -4, 14, -34, 30, 10, 17, -42,
			63, -13, -46, 52, -25, 9, -10, -15, 26, -8, 40, 0, -59, 9, 30, -21,
			36, -3, 8, -13, -16, 25, 31, -59, 6, 37, -13, -15, 25, 42, 1, -66,
			5, 38, 0, 31, -40, 0, -13, 20, 6, 46, -16, -53, 8, 48, -34, 54, -5,
			-11, -27, 52, -64, 48, 16, -7, -1, -55, 25, -15, 42, 14, 1, -50,
			-3, 65, -21, 36, -42, 31, 15, -38, -21, 0, 4, -1, 26, 31, -18, -32,
			54, -31, -9, -3, 30, -30, 43, 30, -62, 21, 0, 37, 15, 0, -10, 3,
			15, -10, -37, -13, 40, -19, 42, -2, 12, -2, -24, -28, 31, -5, 12,
			-31, 1, 25, -26, 61, -6, 18, -27, 24, -48, 13, 49, -65, 44, -12,
			26, -50, 70, -17, -25, 35, -12, 10, -24, -17, 7, 17, 15, 21, -59,
			13, 25, -27, 27, 17, 23, -32, 32, -22, -10, 9, 29, 19, 1, -40, -25,
			64, -56, 28, -8, -16, 55, -18, 38, -15, 20, -21, -26, 38, -62, 32,
			-4, 51, 5, -44, 11, 5, -27, 44, 9, -30, -28, 39, 27, 0, -52, 52,
			13, 3, 5, -38, -6, 40, -52, 51, -13, -9, -15, 48, 22, -38, 32, -13,
			26, -38, 24, -35, 23, -29, 53, -15, -4, -33, 1, 53, 25, -50, 45,
			-32, 33, -30, -28, 34, 28, -30, 47, -37, -11, 0, 45, -27, 32, -33,
			31, -22, -5, 20, 27, -34, -9, 53, -7, -6, -12, -29, 58, -12, -30,
			0, -16, 45, -1, -15, 43, -10, -46, 29, 35, -10, -26, 30, 24, -39,
			31, 0, -53, 50, 24, -51, -24, 36, 47, -55, 45, -5, -32, 9, -29, 47,
			33, 3, -17, -12, 34, -44, 32, 0, 17, -34, 19, 4, 10, -8, 8, 0, -21,
			21, 14, -6, 5, 2, -28, 40, -9, 8, 14, 15, -8, 16, -1, -4, -3, -52,
			55, 14, 1, 8, -13, 11, 8, -68, 68, -1, -61, 36, 19, 4, 10, -8, 8,
			0, -22, 22, 15, -11, 8, 0, 15, -12, 17, -47, 46, 0, 5, 1, 1, -7,
			11, 8, -45, 43, -11, 3, 12, -5, 1, -15, 20, 17, 2, -9, 7, -5, 15,
			-8, 16, -1, -4, -3, -52, 55, 14, 1, 8, -13, 11, 8, -68, 23, 46, 0,
			5, 1, 1, -7, 11, 8, -36, 21, 14, -6, -4, 11, 33, -10, -45, 0, 6,
			16, 4, 8, 35, -16, 0, 18, 5, -27, 13, -5, -18, -5, 20, 12, 9, -64,
			49, 14, -38, -6, 7, 1, -41, 39, 8, 14, 0, 2, -2, 13, 19, -23, 25,
			-24, -11, -2, 2, 25, -34, 30, 5, -9, -55, 62, 11, -2, 1, -62, 30,
			11, -2, 1, -11, 51, 2, -15, 26, -38, 29, -14, 4, -9, 3, 9, 3, 6,
			-40, 11, -2, 1, -11, 51, 2, -15, 26, -12, -29, 56, -16, -6, -18, 2,
			-45, 1, 62, -18, 32, -56, 2, -12, -7, -6, 25, -16, 0, -13, 45, -10,
			6, 35, -21, 9, -48, 54, -16, -7, -18, 2, 0, 17, -38, 23, 15, -5,
			16, -18, 8, 0, 7, 1, -31, 35, 0, -7, 7, -5, 0, 17, -46, 35, 3, -3,
			22, -7, 10, 12, -7, -44, 47, 6, -40, 2, 1, 7, 13, 15, -8, 16, -1,
			-4, -3, -52, 55, 14, 1, 8, -13, 11, 8, -68, 39, 23, -5, 19, -11, 1,
			-18, 36, -11, 3, 1, 15, -11, 11, -9, 4, 16, -77, 35, 33, 7, 13, -3,
			5, 0, 17, -34, 19, 4, 10, -8, 8, 0, -22, 22, 15, -11, 8, 0, 15, 5,
			-9, -55, 62, 11, -2, 1, -62, 30, 11, -2, 1, 3, 21, 10, -3, 0, 19
]

def main_service_cOIcOOo(p0, p1, p2):
	"""
	p6, p7 and p8 are integers
	"""
	v4 = main_service_tab
	v3 = 0
	v2 = 0	
	p2c  = p2 + 0x5f
	v0 = ""
	v5 = 0
	p0c = p0 + 0x2fc
	p1c = p1
	# Tableau qui devrait avoir une taille p0
	count = 0
	v1 = []
	while count < p0c :
		v1.insert(0, 0)
		count += 1

	cond = -1
	while True:
		if (cond == -1) :
			if not (v4 is None):
				cond  = 0
			else :
				v2 = p0c
				v3 = p1c 
				cond =2
		elif (cond == 0):
			v2 = chr(p2c)
			v1[v5] = v2
			v5 += 1
			if (v5 < p0c):
				cond = 1
			else:
				v2 = 0
				for c in v1 :
					v0 = v0 + c	
				return v0
		elif (cond == 1):
			v2 = p2c
			v3 = v4[p1c]
			cond = 2
		elif (cond == 2):
			p1c += 0x1
			p2c = v2 + v3 -2
			cond = 0

program = ""
for line in open("MainService.java"):
	program += line

program = string.replace(program, "\n", "")
program = string.replace(program, "\t", "")
program = string.replace(program, ", ", ",")
program = string.replace(program, " ,", ",")
prg2 = string.split(program)
res = []
new_prg = ""

for s in prg2:
	index = string.find(s, "com.android.system.admin.MainService.cOIcOOo")
	new_s = ""
	if index >= 0:
		left_b = string.find(s, "(", index)
		right_b = string.find(s, ")", left_b + 1)
		if (left_b == -1 or right_b == -1):
			new_s = s
		else:
			param = ""
			i = left_b + 1
			while i < right_b:
				param += s[i]
				i += 1
			param = string.split(param,",")
			class_name = "\"" + main_service_cOIcOOo(int(param[0]), int(param[1]), int(param[2])) + "\""
			if index > 0:
				new_s += s[:index]
			new_s += class_name
			new_s += s[right_b:]
	else:
		new_s = s
	new_prg += new_s + " "

new_prg = string.replace(new_prg, "{", "{\n")
new_prg = string.replace(new_prg, "}", "}\n")
new_prg = string.replace(new_prg, ";", ";\n")

print main_service_cOIcOOo(-760,1167,6)		
